package com.github.catvod.spider.merge;

import com.github.catvod.spider.merge.CR;
import com.github.catvod.spider.merge.Yj;
import com.github.catvod.spider.merge.ms;
import com.github.catvod.spider.merge.v6;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* loaded from: classes.dex */
public class HH {
    private final List<ms> BQ = new ArrayList();
    private final KY I;
    private final String oC;
    private static final String[] h = {",", ">", "+", "~", " "};
    private static final String[] V0 = {"=", "!=", "^=", "$=", "*=", "~="};
    private static final Pattern j = Pattern.compile("(([+-])?(\\d+)?)n(\\s*([+-])?\\s*\\d+)?", 2);
    private static final Pattern q = Pattern.compile("([+-])?(\\d+)");

    private HH(String str) {
        R2.BQ(str);
        String trim = str.trim();
        this.oC = trim;
        this.I = new KY(trim);
    }

    private int BQ() {
        String trim = this.I.V0(")").trim();
        R2.q(ZG.W(trim), "Index must be numeric");
        return Integer.parseInt(trim);
    }

    private void F8() {
        if (this.I.oq("#")) {
            q();
            return;
        }
        if (this.I.oq(".")) {
            j();
            return;
        }
        if (this.I.i4() || this.I.F8("*|")) {
            I();
            return;
        }
        if (this.I.F8("[")) {
            V0();
            return;
        }
        if (this.I.oq("*")) {
            h();
            return;
        }
        if (this.I.oq(":lt(")) {
            i4();
            return;
        }
        if (this.I.oq(":gt(")) {
            iU();
            return;
        }
        if (this.I.oq(":eq(")) {
            n();
            return;
        }
        if (this.I.F8(":has(")) {
            t();
            return;
        }
        if (this.I.F8(":contains(")) {
            u(false);
            return;
        }
        if (this.I.F8(":containsOwn(")) {
            u(true);
            return;
        }
        if (this.I.F8(":containsData(")) {
            Qy();
            return;
        }
        if (this.I.F8(":matches(")) {
            y(false);
            return;
        }
        if (this.I.F8(":matchesOwn(")) {
            y(true);
            return;
        }
        if (this.I.F8(":not(")) {
            PD();
            return;
        }
        if (this.I.oq(":nth-child(")) {
            oq(false, false);
            return;
        }
        if (this.I.oq(":nth-last-child(")) {
            oq(true, false);
            return;
        }
        if (this.I.oq(":nth-of-type(")) {
            oq(false, true);
            return;
        }
        if (this.I.oq(":nth-last-of-type(")) {
            oq(true, true);
            return;
        }
        if (this.I.oq(":first-child")) {
            this.BQ.add(new ms.cy());
            return;
        }
        if (this.I.oq(":last-child")) {
            this.BQ.add(new ms.EE());
            return;
        }
        if (this.I.oq(":first-of-type")) {
            this.BQ.add(new ms.z());
            return;
        }
        if (this.I.oq(":last-of-type")) {
            this.BQ.add(new ms.sy());
            return;
        }
        if (this.I.oq(":only-child")) {
            this.BQ.add(new ms.Sf());
            return;
        }
        if (this.I.oq(":only-of-type")) {
            this.BQ.add(new ms.pK());
            return;
        }
        if (this.I.oq(":empty")) {
            this.BQ.add(new ms.A());
        } else if (this.I.oq(":root")) {
            this.BQ.add(new ms.s());
        } else {
            if (!this.I.oq(":matchText")) {
                throw new CR.Tw("Could not parse query '%s': unexpected token at '%s'", this.oC, this.I.y());
            }
            this.BQ.add(new ms.uJ());
        }
    }

    public static ms GM(String str) {
        try {
            return new HH(str).j5();
        } catch (IllegalArgumentException e) {
            throw new CR.Tw(e.getMessage(), new Object[0]);
        }
    }

    private void I() {
        String V02 = AR.V0(this.I.oC());
        R2.BQ(V02);
        if (V02.startsWith("*|")) {
            this.BQ.add(new v6.yh(new ms.aF(V02), new ms.JL(V02.replace("*|", ":"))));
            return;
        }
        if (V02.contains("|")) {
            V02 = V02.replace("|", ":");
        }
        this.BQ.add(new ms.aF(V02));
    }

    private void PD() {
        this.I.q(":not");
        String h2 = this.I.h('(', ')');
        R2.W(h2, ":not(selector) subselect must not be empty");
        this.BQ.add(new Yj.d(GM(h2)));
    }

    private void Qy() {
        this.I.q(":containsData");
        String j5 = KY.j5(this.I.h('(', ')'));
        R2.W(j5, ":containsData(text) query must not be empty");
        this.BQ.add(new ms.H(j5));
    }

    private void V0() {
        KY ky = new KY(this.I.h('[', ']'));
        String W = ky.W(V0);
        R2.BQ(W);
        ky.u();
        if (ky.Qy()) {
            if (W.startsWith("^")) {
                this.BQ.add(new ms.d(W.substring(1)));
                return;
            } else {
                this.BQ.add(new ms.yh(W));
                return;
            }
        }
        if (ky.oq("=")) {
            this.BQ.add(new ms.c(W, ky.y()));
            return;
        }
        if (ky.oq("!=")) {
            this.BQ.add(new ms.j(W, ky.y()));
            return;
        }
        if (ky.oq("^=")) {
            this.BQ.add(new ms.i(W, ky.y()));
            return;
        }
        if (ky.oq("$=")) {
            this.BQ.add(new ms.qM(W, ky.y()));
        } else if (ky.oq("*=")) {
            this.BQ.add(new ms.W(W, ky.y()));
        } else {
            if (!ky.oq("~=")) {
                throw new CR.Tw("Could not parse attribute query '%s': unexpected token at '%s'", this.oC, ky.y());
            }
            this.BQ.add(new ms.x(W, Pattern.compile(ky.y())));
        }
    }

    private String W() {
        StringBuilder V02 = ZG.V0();
        while (!this.I.Qy()) {
            if (this.I.F8("(")) {
                V02.append("(");
                V02.append(this.I.h('(', ')'));
                V02.append(")");
            } else if (this.I.F8("[")) {
                V02.append("[");
                V02.append(this.I.h('[', ']'));
                V02.append("]");
            } else {
                if (this.I.n(h)) {
                    break;
                }
                V02.append(this.I.j());
            }
        }
        return ZG.t(V02);
    }

    private void h() {
        this.BQ.add(new ms.Tw());
    }

    private void i4() {
        this.BQ.add(new ms.TL(BQ()));
    }

    private void iU() {
        this.BQ.add(new ms.E(BQ()));
    }

    private void j() {
        String I = this.I.I();
        R2.BQ(I);
        this.BQ.add(new ms.kb(I.trim()));
    }

    private void n() {
        this.BQ.add(new ms.BU(BQ()));
    }

    /* JADX WARN: Removed duplicated region for block: B:21:0x00c7  */
    /* JADX WARN: Removed duplicated region for block: B:25:0x00ce  */
    /* JADX WARN: Removed duplicated region for block: B:33:0x00b4  */
    /* JADX WARN: Removed duplicated region for block: B:9:0x0046  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private void oC(char r11) {
        /*
            Method dump skipped, instructions count: 213
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: com.github.catvod.spider.merge.HH.oC(char):void");
    }

    private void oq(boolean z, boolean z2) {
        String V02 = AR.V0(this.I.V0(")"));
        Matcher matcher = j.matcher(V02);
        Matcher matcher2 = q.matcher(V02);
        int i = 2;
        if ("odd".equals(V02)) {
            r5 = 1;
        } else if (!"even".equals(V02)) {
            if (matcher.matches()) {
                int parseInt = matcher.group(3) != null ? Integer.parseInt(matcher.group(1).replaceFirst("^\\+", "")) : 1;
                r5 = matcher.group(4) != null ? Integer.parseInt(matcher.group(4).replaceFirst("^\\+", "")) : 0;
                i = parseInt;
            } else {
                if (!matcher2.matches()) {
                    throw new CR.Tw("Could not parse nth-index '%s': unexpected format", V02);
                }
                r5 = Integer.parseInt(matcher2.group().replaceFirst("^\\+", ""));
                i = 0;
            }
        }
        if (z2) {
            if (z) {
                this.BQ.add(new ms.u(i, r5));
                return;
            } else {
                this.BQ.add(new ms.lW(i, r5));
                return;
            }
        }
        if (z) {
            this.BQ.add(new ms.jH(i, r5));
        } else {
            this.BQ.add(new ms.V(i, r5));
        }
    }

    private void q() {
        String I = this.I.I();
        R2.BQ(I);
        this.BQ.add(new ms.RN(I));
    }

    private void t() {
        this.I.q(":has");
        String h2 = this.I.h('(', ')');
        R2.W(h2, ":has(el) subselect must not be empty");
        this.BQ.add(new Yj.Tw(GM(h2)));
    }

    private void u(boolean z) {
        this.I.q(z ? ":containsOwn" : ":contains");
        String j5 = KY.j5(this.I.h('(', ')'));
        R2.W(j5, ":contains(text) query must not be empty");
        if (z) {
            this.BQ.add(new ms.q(j5));
        } else {
            this.BQ.add(new ms.p(j5));
        }
    }

    private void y(boolean z) {
        this.I.q(z ? ":matchesOwn" : ":matches");
        String h2 = this.I.h('(', ')');
        R2.W(h2, ":matches(regex) query must not be empty");
        if (z) {
            this.BQ.add(new ms.jP(Pattern.compile(h2)));
        } else {
            this.BQ.add(new ms.oe(Pattern.compile(h2)));
        }
    }

    ms j5() {
        this.I.u();
        if (this.I.n(h)) {
            this.BQ.add(new Yj.qM());
            oC(this.I.j());
        } else {
            F8();
        }
        while (!this.I.Qy()) {
            boolean u = this.I.u();
            if (this.I.n(h)) {
                oC(this.I.j());
            } else if (u) {
                oC(' ');
            } else {
                F8();
            }
        }
        return this.BQ.size() == 1 ? this.BQ.get(0) : new v6.Tw(this.BQ);
    }

    public String toString() {
        return this.oC;
    }
}
